/**
 * 适配器，适配列数据，把旧属性适配到新属性上

 */
import XEUtils from 'xe-utils';

export const adapterColumns = (obj) => {
  let tableSetting = obj.tableSetting;
  let aryCols = obj.gridColumnData;
  let validRules = {};
  let cloneCols = XEUtils.clone(aryCols, true),
    formItems = [];
  aryCols.forEach((colItem, i) => {
    let colConfig = {};
    if (colItem.config) {
      try {
        colConfig = JSON.parse(colItem.config);
      } catch (e) {
        colConfig = {};
        console.log(colItem.config + " 不符合json串规范");
      }
    }
    if (colItem.requirement) {
      validRules[colItem.code] = [{
        required: true,
        message: colItem.name + '是必填项！'
      }]
      if (colItem.datalength) {
        validRules[colItem.code].push({
          min: 0,
          max: colItem.datalength,
          message: `名称长度在 0 到 ${colItem.datalength} 个字符`
        });
      }
    }

    colItem["field"] = colItem.code;
    colItem["headerAlign"] = colConfig["headerAlign"] || "center"; // 表头文字位置
    if (colConfig["align"]) { // 列位置
      colItem.align = colConfig["align"];
    }
    colItem["width"] = colItem.colWidth;
    console.log('colItem["width"]',colItem["width"])
    colItem["visible"] = colItem.isVisiable ? true : false;
    if (colItem.isFilter && colItem.type == "input") {
      colItem["filters"] = [{
        data: null
      }]
      colItem["filterRender"] = {
        name: 'FilterInput'
      }
    }
    if (colItem.isFilter && colItem.type == "number") {
      colItem["filters"] = [{
        data: {
          type: 'has',
          isCase: true,
          name: ''
        }
      }]
      colItem["filterRender"] = {
        name: 'FilterComplex'
      }
    }
    if (colItem.type == "select") {
      colItem["formatter"] = "formatSelect";
    } else if (colItem.type == "amt") {
      colItem["formatter"] = ["formatAmount"];
    }
    //可编辑的单元格
    if (tableSetting.isEdit && colItem.isEdit) {
      if (colItem.type == "input") {
        colItem["editRender"] = {
          name: "input",
          defaultValue: colItem.defaultvalue,
          props: {
            type: "text"
          }
        };
      } else if (colItem.type == "number" || colItem.type == "amt") {
        colItem["editRender"] = {
          name: "input",
          defaultValue: colItem.defaultvalue,
          attrs: {
            type: colItem.type
          }
        };
        colItem.align = colConfig["align"] || 'right'; // 金额和数字默认居右
      } else if (colItem.type == "switch") {
        colItem["cellRender"] = {
          name: "ElSwitch",
          props: {
            activeValue: (colConfig["activeValue"] || 1),
            inactiveValue: (colConfig["inactiveValue"] || 0)
          },
          events: {
            //change回调方法
            change: function (dg, val) {
              obj.$emit("switchChange_" + colItem["field"] + "_" + obj.which, dg, val); // 添加change回调方法
            }

          }
        };
      } else if (colItem.type == "select") {
        colItem["editRender"] = {
          name: "ElSelect",
          defaultValue: colItem.defaultvalue,
          options: [],
          optionProps: {
            value: 'dataId',
            label: 'label'
          },
          events: {
            change: function (dg, val) { // select切换事件时，赋值到行对象
              let data = dg.column.editRender.options;
              let eleCode = colItem["field"] + "Obj";
              dg.row[eleCode] = data.find(item => item.dataId === val);
              obj.$emit("selectChange_" + colItem["field"] + "_" + obj.which, dg, val); // 添加change回调方法
            }
          }
        };
        colItem.align = colConfig["align"] || 'left';
      } else if (colItem.type == "textarea") {
        colItem["editRender"] = {
          name: colItem.type,
          defaultValue: colItem.defaultValue
        };
      } else if (colItem.type == "date") {
        colItem["editRender"] = {
          name: "ElDatePicker",
          defaultValue: colItem.defaultValue,
          props: {
            type: colItem.type,
            format: "yyyy/MM/dd"
          }
        };
      } else if (colItem.type == "tree") {
        colItem["editRender"] = {
          name: "myTree",
          defaultValue: colItem.defaultValue
        };

      } else if (colItem.type == "myCheckbox") {
        colItem["editRender"] = {
          name: "myCheckbox",
          defaultValue: colItem.defaultValue,
          events: {
            change: function (row, column) {
              row[column.property] = !row[column.property];

            }
          }
        };
      } else if (colItem.type == "treeModel") {
        // colItem["editRender"] = {name:"myTreeModel", defaultValue: colItem.defaultValue,cacheData:obj._cacheData,fromSource:colItem.fromSource};
      } else if (colItem.type == "radio") {
        colItem["editRender"] = {
          name: "myradio",
          defaultValue: colItem.defaultValue,
          props: colItem.config,
          events: {
            change: function (row, column) {
              row[column.property] = row[column.property] == 0 ? 1 : 0;
            }
          }
        };
      } else if (colItem.type == "jsonList") {
        colItem["editRender"] = {
          name: "myJsonList",
          defaultValue: colItem.defaultValue,
          props: colItem.config
        };
      }

    }
  })
  if (tableSetting.config.operate) {
    aryCols.push({
      field: 'name',
      title: '操作列',
      width: 160,
      fixed: 'right',
      slots: {
        default: 'operateCol'
      }
    });
  }
  cloneCols.forEach((colItem, i) => {
    //只有可用(显示)的才新增的列
    if (colItem.isVisiable && colItem.type != 'seq' && colItem.type != 'checkbox') {
      formItems.push(colItem);
      if (colItem.type == "input") {
        colItem.itemRender = {
          name: "$input",
          props: {
            placeholder: `请输入${colItem.name}`
          }
        };
      } else if (colItem.type == "number" || colItem.type == "amt") {
        colItem.itemRender = {
          name: "$input",
          props: {
            type: 'number',
            placeholder: `请输入${colItem.name}`
          }
        };
      } else if (colItem.type == "switch") {
        colItem.itemRender = {
          name: "ElSwitch"
        };
      } else if (colItem.type == "select") {
        colItem.itemRender = {
          name: "select",
          options: []
        };
      } else if (colItem.type == "textarea") {
        colItem.titleSuffix = {
          message: '提示信息！！',
          icon: 'fa fa-question-circle'
        };
        colItem.itemRender = {
          name: "$" + colItem.type,
          props: {
            placeholder: `请输入${colItem.name}`
          }
        };
      } else if (colItem.type == "date") {
        colItem.itemRender = {
          name: "$input",
          props: {
            type: colItem.type,
            format: "yyyy/MM/dd",
            placeholder: `请输入${colItem.name}`
          }
        };
      } else if (colItem.type == "tree") {
        colItem.itemRender = {
          name: "FormMyTree"
        };
      }
      colItem.span = colItem.span || 12;
    }
  })
  formItems.push({
    align: 'center',
    span: 24,
    titleAlign: 'left',
    itemRender: {
      name: '$buttons',
      children: [{
        props: {
          type: 'submit',
          content: '提交',
          status: 'primary'
        }
      }, {
        props: {
          type: 'reset',
          content: '重置'
        }
      }]
    }
  });

  return {
    aryCols,
    validRules,
    formItems
  };
}
